/**
 * 依赖于jQuery,插件的类名和id与模板对应
 * [使用方法：传入options替换默认参数和默认规则]
 * @param  {[type]} root    [根作用域window]
 * @param  {[type]} factory [工厂函数]
 * @param  {[type]} plug    [verifyData]
 */
( function( root, factory, plug ) {
	factory( root.jQuery, plug );
} )( window, function( $, plug ) {
	//默认参数
	var _DEFAULTS_ = {
		"plugName": "dr",
		"newPlungName": "",
		"type": [ "text", "checkbox", "password" ]//获得焦点的类型
	}
	//默认规则
	var _RULES_ = {
		"regexp": function( data ) {
			if( !this.val() ){ return true }
			return new RegExp( data ).test( this.val() );
		},
		"maxlength": function( data ) {
			return this.val().length <= data;
		},
		"minlength": function( data ) {
			return this.val().length >= data;
		}
	}
	//信息
	var _MESSAGES_ = {
		"username": {
			"required": true,
			"required-message": "请输入用户名",
			"regexp": "(?!(\\d)+$)^[\\u4e00-\\u9fa5\\w]+$",
			"regexp-message": "用户名仅支持中英文、数字和下划线,且不能为纯数字",
			"maxlength": 14,
			"maxlength-message": "用户名不能超过7个汉字或14个字符"
		},
		"phone": {
			"required": true,
			"required-message": "请输入手机号",
			"regexp": "^(13|14|15|18)[0-9]{9}$",
			"regexp-message": "手机号码格式不正确"
		},
		"verifyCode": {
			"required": true,
			"required-message": "请您输入验证码"
		},
		"isAgree": {
			"required": true,
			"required-message": "请勾选“阅读并接受xx用户协议”"
		},
		"password": [
			{ "className": "nopwd", "title": "","detail": "请输入密码" },
			{ "className": "weak", "title": "弱","detail": "请重新设置，试试数字、字母、符号组合" },
			{ "className": "middle", "title": "中","detail": "您的密码还可以更复杂些" },
			{ "className": "strong", "title": "强","detail": "您的密码很安全,请牢记！" }
		]
	}

	//补充配置
	var class2 = {
		"required": function( data ) {
			return this.val() === "on" ? this.is(":checked") : this.val();
		},
		"getStrength": function( data ) {
			var s = 0,
				val = this.val();

			if( /[a-zA-Z]/.test(val) ) { s++ };
			if( /[0-9]/.test(val) ) { s++ };
			if( /[`\-=\[\];,\.\/~!@#\$%\^*\(\)_+\}\{:\?]/.test(val) ) { s++ };
			if( !val ) { s = 0 };
			this.siblings(".form-error").css({"display": "inline"}).find(".pwd-strength").addClass( data[s].className )
				.find(".pwd-strength-detail").text( data[s].detail )
				.parent().find(".pwd-strength-title").text( data[s].title );
			return s;
		},
		"opmessage": function( root, key, fn ) {
			var $obj = root[this.data( root.plugName )],
				$rule = $obj[ key ];
				formerror = this.parent().find(".form-error");
			if( this[0].type === "password" ) {
				var s = root.getStrength.call( this, $obj );
				if( s < 2 ) { this.addClass( "form-text-input-error" ); }
				return;
			}
			if( $rule ) {	
				var $result = fn.call( this, $rule ),
					$message = $obj[ key + "-message" ],
					$formsucc = this.siblings(".form-succ");
				if( !$result ) {
					$formsucc.css({"display": "none"});
					this.addClass( "form-text-input-error" );
					if( $message ) {
						formerror.css({"display": "inline"}).text( $message );
					}
				} else {
					console.log(fn)
					$formsucc.css({"display": "inline"});
				}
			}
		},
		"opNewMessage": null
	};
	$.fn.extend( class2, _DEFAULTS_, _RULES_, _MESSAGES_ );

	$.fn[plug] = function( options ) {
		if( !this.is( "form" ) ) { return }
		var $input = this.find( "input" );
		$.fn.extend( this, class2, options );

		var _this = this;

		$input.on( "focus", function() {
			if( _this.type.indexOf( this.type ) < 0 ){ return }
			var $this = $(this),
				clearbtn = $this.siblings( ".form-clearbtn" ),
				formtip = $this.siblings(".form-tip"),
				formsucc = $this.siblings(".form-succ"),
				formerror = $this.parent().find(".form-error");

			$this.removeClass( "form-text-input-error" );
			$this.addClass( "form-text-input-focus" );
			formsucc.css({"display": "none"})

			this.type === "password" ? 
			formerror.css({"display": "none"}).find(".pwd-strength").attr( {"class": "pwd-strength"} ) : 
			formerror.css({"display": "none"}).text( "" );

			if( $this[0].id !== "veri-verifyCode" ) {
				formtip.css({"display": "inline"});
			}

			$this.on( "input", function() {
				this.value ? clearbtn.css( {"display": "block", "visibility": "visible"} ) : clearbtn.css({"display": "none", "visibility": "hidden"});
				if( this.type === "password" && !this.value ) {
					$this.parent().find(".form-tip-password").find(".pwd-checklist-item").removeClass("pwd-checklist-item-success").removeClass("pwd-checklist-item-error");
					return;
				}
				var newData = $this.data( _this.newPlungName );
				if( newData ) {
					$.each( _RULES_, function( key, fn ) {
						_this.opNewMessage.call( $this, _this, newData, key, fn );
					} );
				}
			} );

			clearbtn.on( "click", function() {
				$this.val( "" );
				formsucc.css({"display": "none"});
				clearbtn.css({"display": "none", "visibility": "hidden"});
			} );

			$this.on( "blur",function() {
				$this.removeClass( "form-text-input-focus" );
				formtip.css({"display": "none"});
				if( !this.value ) { return }
				$.each( _RULES_, function( key, fn ) {
					_this.opmessage.call( $this, _this, key, fn );
				} );
			} );
		} );
		this.on( "submit",function() {
			for( var i = 0; i < $input.length; i++ ) {
				if( !$($input[i]).data( _this.plugName ) ) { continue }
				_this.opmessage.call( $($input[i]), _this, "required", _this.required );
			}
		} );
	}
	$.fn[plug].extendRules = function( options ) {
		$.extend( _RULES_, options );
	}
}, "verifyData" );